package com.opengroupe.recognize;

import java.io.IOException;
import java.io.InputStream;

import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.Log;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;

/**
 * La classe utilitaire.
 * @author HAM11235
 *
 */
public class Utility {

	/** log tag.*/
	private static String LOG_TAG = "Utility";

	/**
	 * récupére l'id de la camera du front.
	 * @return
	 */
	public static int findFrontFacingCamera() {
		int cameraId = -1;
		int numberOfCameras = Camera.getNumberOfCameras();
		for (int i = 0; i < numberOfCameras; i++) {
			CameraInfo info = new CameraInfo();
			Camera.getCameraInfo(i, info);
			if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
				Log.d(LOG_TAG, "Camera trouver.");
				cameraId = i;
				break;
			}
		}
		return cameraId;
	}

	/**
	 * récupére l'id de la camera du back.
	 * @return
	 */
	public static int findBackFacingCamera() {
		int cameraId = -1;
		int numberOfCameras = Camera.getNumberOfCameras();
		for (int i = 0; i < numberOfCameras; i++) {
			CameraInfo info = new CameraInfo();
			Camera.getCameraInfo(i, info);
			if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
				Log.d(LOG_TAG, "Camera trouver.");
				cameraId = i;
				break;
			}
		}
		return cameraId;
	}
	
	/**
	 * 
	 * @param val
	 * @return
	 */
	public static boolean estVide(String val){
		if(val != null && (new String()).equals(val))
			return true;
		else if (val == null)
			return true;
		else return false;
	}
	
	/**
	 * récupération du préfixe du fichier.
	 * @param filename
	 * @return
	 */
	public static String getFilePrefix(String filename) {
		String[] fileNameSplitted = filename.split(".");
		String prefix = new String();
		for(int i = 0; i<fileNameSplitted.length -1; i++)
			prefix = prefix + fileNameSplitted[i];
		return prefix; //fileNameSplitted[fileNameSplitted.length - 1];
	}
	
	/**
	 * téléchargement du fichier à partir du drive de google.
	 * @param file
	 * @return
	 */
	public static InputStream downloadFile(com.google.api.services.drive.model.File file){
		//download the file
		HttpRequest req;
		InputStream  is = null;
		try {
			req = MainActivity.service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()));
			req.setContentLoggingLimit(0);
			HttpResponse resp = req.execute();
			is = resp.getContent();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return is;
	}

}
